package com.example.chatapp.controller;

import com.example.chatapp.common.ApiResponse;
import com.example.chatapp.common.BusinessException;
import com.example.chatapp.service.AuthService;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 * 职责：处理HTTP请求，调用Service，返回统一格式响应
 */
@RestController
@RequestMapping("/auth")
public class AuthController {
    
    private final AuthService authService;
    
    public AuthController(AuthService authService) {
        this.authService = authService;
    }
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ApiResponse<String> register(@RequestParam String username,
                                       @RequestParam String password) {
        // 调用Service进行注册
        authService.register(username, password);
        
        return ApiResponse.success("注册成功");
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ApiResponse<String> login(@RequestParam String username,
                                     @RequestParam String password,
                                     @RequestParam(defaultValue = "default") String roomId,
                                     HttpSession session) {
        // 验证用户名和密码
        authService.authenticate(username, password);
        
        // 调用Service
        authService.login(username, roomId);
        
        // 设置Session
        session.setAttribute("username", username);
        session.setAttribute("roomId", roomId);
        
        // 返回统一格式
        return ApiResponse.success("登录成功");
    }
    
    /**
     * 切换房间
     */
    @PostMapping("/switchRoom")
    public ApiResponse<String> switchRoom(@RequestParam String roomId,
                                         HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username == null) {
            throw new BusinessException(401, "未登录");
        }
        
        // 调用Service
        authService.switchRoom(username, roomId);
        
        // 更新Session
        session.setAttribute("roomId", roomId);
        
        return ApiResponse.success("切换房间成功");
    }
    
    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public ApiResponse<String> logout(HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username != null) {
            // 调用Service
            authService.logout(username);
            
            // 清除Session
            session.invalidate();
        }
        
        return ApiResponse.success("登出成功");
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/current")
    public ApiResponse<Map<String, Object>> getCurrentUser(HttpSession session) {
        String username = (String) session.getAttribute("username");
        String roomId = (String) session.getAttribute("roomId");
        
        if (username == null) {
            throw new BusinessException(401, "未登录");
        }
        
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("username", username);
        userInfo.put("roomId", roomId);
        userInfo.put("online", authService.isUserOnline(username));
        
        return ApiResponse.success(userInfo);
    }
    
    /**
     * 获取指定房间的在线用户列表
     */
    @GetMapping("/online-users")
    public ApiResponse<java.util.List<String>> getOnlineUsers(@RequestParam String roomId) {
        java.util.List<String> onlineUsers = authService.getOnlineUsersInRoom(roomId);
        return ApiResponse.success(onlineUsers);
    }
}
